var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
var pageNum =1;
function searchList(pageNum) {
    $.ajax({
        url: root.couponInter.coupon_querySendCoupon,
        // url:'http://192.168.7.100:8087/querySendCoupon',
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            "sendCouponCode": $('#sendCouponCode').val(),
            "sendCouponName": $('#sendCouponName').val(),
            "sendCustType": $('#sendCustType').val(),
            "sendCouponDept": $('#sendCouponDept').val(),
            "sendCouponUser": $('#sendCouponUser').val(),
            "sendCouponType": $('#sendCouponType').val(),
            "sendReasonDutyDept": $('#sendReasonDutyDept').val(),
            "sendCouponReason": $('#sendCouponReason').val(),
            "auditStatus": $('#auditStatus').val(),
            "auditPerson": $('#auditPerson').val(),
            "inputStarTime": $('#createStartTimeValue').val(),
            "inputEndTime": $('#createEndTimeValue').val(),
            "activeStartTimeValue": $('#logmin').val(),
            "activeEndTimeValue": $('#logmax').val(),
            "pageNum": pageNum,
            "pageSize": 10,
        }),
        success: function (data) {
            console.log(data);
            couponlist(data.result);
            $('.M-box3').pagination({
                pageCount: data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.couponInter.coupon_querySendCoupon,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType: 'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "sendCouponCode": $('#sendCouponCode').val(),
                            "sendCouponName": $('#sendCouponName').val(),
                            "sendCustType": $('#sendCustType').val(),
                            "sendCouponDept": $('#sendCouponDept').val(),
                            "sendCouponUser": $('#sendCouponUser').val(),
                            "sendCouponType": $('#sendCouponType').val(),
                            "sendReasonDutyDept": $('#sendReasonDutyDept').val(),
                            "sendCouponReason": $('#sendCouponReason').val(),
                            "auditStatus": $('#auditStatus').val(),
                            "auditPerson": $('#auditPerson').val(),
                            "inputStarTime": $('#createStartTimeValue').val(),
                            "inputEndTime": $('#createEndTimeValue').val(),
                            "activeStartTimeValue": $('#logmin').val(),
                            "activeEndTimeValue": $('#logmax').val(),
                            "pageNum": api.getCurrent(),
                            "pageSize": 10,
                        }),
                        success: function (result) {
                            console.log(result)
                            couponlist(result.result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function (data) {
            console.log(data);
        }
    });
}

function couponlist(data){
console.log(data)
    dataSource.splice(0,dataSource.length);//清空数组
    $.each(data, function (i, field) {
        var rowData = new Array();
        rowData.push(field.sendCouponCode);
        rowData.push(field.sendCouponName);
        rowData.push(field.customerType);
        rowData.push(field.sendCouponDept);
        rowData.push(field.sendCouponUser);
        rowData.push(field.sendCouponReason);
        rowData.push(field.sendCouponType);
        rowData.push(field.auditStatus);
        rowData.push(field.createdAt);
        rowData.push(field.auditPerson);
        rowData.push(field.auditTime);
        rowData.push(field.sendCouponId);
        rowData.push(field.token);
        rowData.push(field.sendCondition);
        rowData.push(field.startTime);
        rowData.push(field.sendCustType);
        rowData.push(field.endTime);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[ 8, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu": [20,40,60],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var custArr = [];
                            if(row[2]!='' && row[2]!=null){
                                custArr = row[2].split(",");
                            }
                            var val = '';
                        if(row[6] == 1){
                            if(row[15]==11){
                                val = '签约客户(非分销)';
                            }else if(row[15]==12){
                                val = '签约客户(分销)';
                            }else if(row[15]==20){
                                val = '普通客户';
                            }else if(row[15]==30){
                                val = '认证客户';
                            }
                        }else {
                            for (var i = 0; i < custArr.length; i++) {
                                console.log(custArr[i]);
                                if (custArr[i] == '') {
                                    val += '';
                                }
                                if (custArr[i] == 11) {
                                    val += '签约客户(非分销) </br>';
                                }
                                if (custArr[i] == 12) {
                                    val += '签约客户(分销) </br>';
                                }
                                if (custArr[i] == 20) {
                                    val += '普通客户 </br>';
                                }
                                if (custArr[i] == 30) {
                                    val += '认证客户 </br>';
                                }
                                if (custArr[i] == 40) {
                                    val += '签约客户(非分销)</br> 签约客户(分销)</br> 普通客户</br> 认证客户</br> ';
                                }
                            }
                            console.log(val);
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[6] == 1) {
                            val = '手动发券'
                        }
                        else if (row[6] == 2 && row[13] == 0) {
                            val = '自动发券'
                        }
                        else if (row[6] == 2 && row[13] == 1) {
                            val = '自动发券</br> 新用户注册 </br>'
                        }
                        else if (row[6] == 2 && row[13] == 2) {
                            val = '自动发券</br> 满足订单金额条件</br>'
                        } else if (row[6] == 2 && row[13] == 4) {
                            val = '自动发券</br> 按照客户的价格类型</br>'
                        }
                        else if (row[6] == 3) {
                            val = '客户领取发券'
                        }else if (row[6] == 4) {
                            val = '抽奖发送'
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[6] == 1) {
                            if(row[7]==10){
                                val = '审核通过即时生效';
                            }else{
                                val = row[10];
                            }
                        }else{
                            if(row[16]==null || row[16]==''){
                                val = ''+row[14]+'</br> '+'~'+'</br>'+'不限'+'</br>';
                            }else{
                                val = ''+row[14]+'</br> '+'~'+'</br>'+row[16]+'</br>';
                            }

                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        val = row[3]
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        val = row[4]
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[5] == 11) {
                            val = '质量问题'
                        } else if (row[5] == 12) {
                            val = '无货问题'
                        } else if (row[5] == 21) {
                            val = '配送延迟'
                        } else if (row[5] == 22) {
                            val = '状态维护错误'
                        } else if (row[5] == 23) {
                            val = '私自拒收'
                        } else if (row[5] == 24) {
                            val = '包装/商品破损'
                        } else if (row[5] == 25) {
                            val = '违规收费'
                        } else if (row[5] == 26) {
                            val = '服务态度差'
                        } else if (row[5] == 27) {
                            val = '错漏发'
                        } else if (row[5] == 31) {
                            val = '退款时效慢'
                        } else if (row[5] == 32) {
                            val = '发票信息有误'
                        } else if (row[5] == 33) {
                            val = '退款金额有误'
                        } else if (row[5] == 34) {
                            val = '开具发票时间延迟'
                        } else if (row[5] == 41) {
                            val = '商品参数错误'
                        } else if (row[5] == 42) {
                            val = '价格错误'
                        } else if (row[5] == 43) {
                            val = '活动与实际不符'
                        } else if (row[5] == 44) {
                            val = '活动优惠券补发'
                        } else if (row[5] == 45) {
                            val = '活动优惠券发放'
                        } else if (row[5] == 51) {
                            val = '特殊售后问题'
                        } else if (row[5] == 52) {
                            val = '提升客户体验'
                        } else if (row[5] == 53) {
                            val = '承诺错误'
                        } else if (row[5] == 54) {
                            val = '售后时间长'
                        } else if (row[5] == 61) {
                            val = '售后运费补偿'
                        } else if (row[5] == 62) {
                            val = '错发运费补偿'
                        } else if (row[5] == 71) {
                            val = '订单未流转'
                        } else if (row[5] == 72) {
                            val = '活动未绑定优惠券、赠品'
                        } else if (row[5] == 73) {
                            val = '系统自动取消'
                        } else if (row[5] == 74) {
                            val = '页面缓存问题'
                        }
                        return val;
                    },
                    "targets": 7
                }, {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[7] == 10) {
                            val = '待审核'
                        }
                        else if (row[7] == 20) {
                            val = '已生效'
                        } else if (row[7] == 30) {
                            val = '审核不通过'
                        } else if (row[7] == 40) {
                            val = '已失效'
                        }
                        else if (row[7] == 50) {
                            val = '已生效'
                        }
                        else if (row[7] == 60) {
                            val = '未生效'
                        }
                        else if (row[7] == 70) {
                            val = '强制失效'
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row) {
                        if (row[8] == '1000-01-01 00:00:00') {
                            var val = "";
                            return val;
                        } else {
                            var val = '<span>' + row[8] + '</span>';
                            return val;
                        }
                    },
                    "targets": 9
                },
                {
                    "render": function (data, type, row) {
                        var val = row[9];
                        return val;
                    },
                    "targets": 10
                },
                {
                    "render": function (data, type, row) {
                        var val = row[10];
                        return val;
                    },
                    "targets": 11
                },
                {
                    "render": function (data, type, row, meta) {
                        //手动发券
                        if(row[6] == 1 && row[7] == 10){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 156" style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 50){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 156" style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 20){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 30){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 40){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 60){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 1 && row[7] == 70){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        //自动发券
                        if(row[6] == 2 && row[7] == 10){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 156" style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 2 && row[7] == 60){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 2 && row[7] == 50){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 2 && row[7] == 40){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 2 && row[7] == 20){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 2 && row[7] == 30){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 2 && row[7] == 70){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        //客户领取发券
                        if(row[6] == 3 && row[7] == 10){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 156" style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 60){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 50){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 486"><span class="list" onclick="chesddck(\'复制推广\',\'coupon-coupon-query-replicationextension.html\',\'700\',\'400\',\'' + row[11] + '\')">复制推广</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 40){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 20){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 30){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 3 && row[7] == 70){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }

                        //抽奖发送
                        if(row[6] == 4 && row[7] == 10){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 156" style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 4 && row[7] == 60){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 4 && row[7] == 50){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="list powerButton 485" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 4 && row[7] == 40){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }else if(row[6] == 4 && row[7] == 20){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 4 && row[7] == 30){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }
                        else if(row[6] == 4 && row[7] == 70){
                            var val = '<div class="action" id="action' + row[11] + '"><a class="powerButton 157"><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a></div>'
                            return val
                        }


                       /* if (row[7] == 10) {
                            var val = '<div class="action" id="action' + row[11] + '"><a style="margin-right: 20px;"><span class="list" id="' + row[11] + '" onclick="change(\'发券修改\',\'coupon-coupon-query-change.html\',\'' + row[11] + '\',\'' + row[12] + '\')">修改</br></span></a><a><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a><a><span class="list" onclick="chesddck(\'复制推广\',\'coupon-coupon-query-replicationextension.html\',\'700\',\'400\',\'' + row[11] + '\')">复制推广</br></span></a></div>'
                            return val
                        }
                        else {
                            var val = '<div class="action" id="action' + row[11] + '"><a style="margin-right: 20px;color: #999;"><span class="list" id="' + row[11] + '">修改</br></span></a><a><span class="list" onclick="check(\'发券查看\',\'coupon-coupon-query-check.html\',\'' + row[11] + '\')">查看</br></span></a><a class="list" id="'+row[11]+'" onclick="mandatoryfailur('+row[11]+')">强制失效</br></a></div>'
                            return val
                        }*/

                    },
                    "targets": 12
                },
            ]
        });
        //权限控制函数
        power()
    };
}
function change(title,url,sendCouponId,token) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url +'?sendCouponId='+sendCouponId
    });
    layer.full(index);
}
function check(title,url,sendCouponId) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url +'?sendCouponId='+sendCouponId
    });
    layer.full(index);
}
function chesddck(title,url,w,h,sendCouponId){
    console.log(sendCouponId)
    layer_show(title,url+'?sendCouponId='+sendCouponId,w,h);
}
//强制失效

function mandatoryfailur(id) {
    console.log(id)
    $.ajax({
        url:root.couponInter.sendCouponForceInvalid,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: {
            sendCouponId:id,
            forceInvalidPerson:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name

        },
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert('成功！',2000);
                searchList(1)
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })
}
var flag1 = true;
$('#sendCouponDept').on('compositionstart',function(){
    flag1 = false;
})
$('#sendCouponDept').on('compositionend',function(){
    flag1 = true;
})
$('#sendCouponDept').on('input',function(){
    var _this = this;
    setTimeout(function(){
        if(flag1){
            var value=$(_this).val();
            $('li').remove();
            $.ajax({
                url: root.accessInter.access_department_listDepartment,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                data:{
                    "keywords":value
                },
                success: function (data) {
                    console.log(data);
                    if(value!='')
                    {
                        $(".deptlist").removeClass('hide');
                        for (var i in data.bianmaList) {
                            $("#deptList").append('<li id="'+data.bianmaList[i].BIANMA+'" class="depts">' + data.bianmaList[i].DEPARTMENT_NAME + '</li>');
                        }
                    }
                    else{
                        document.getElementsByClassName('deptlist')[0].classList.add('hide');
                    }
                    $('.depts').on('click',function () {
                        $('#sendCouponDept').val(this.innerHTML);
                    })
                }
            });
        }
    },0)
})

var flag2 = true;
$('#sendCouponUser').on('compositionstart',function(){
    flag2 = false;
})
$('#sendCouponUser').on('compositionend',function(){
    flag2 = true;
})
$('#sendCouponUser').on('input',function(){
    var _this = this;
    setTimeout(function(){
        if(flag2){
            var value=$(_this).val();
            $('li').remove();
            $.ajax({
                url: root.accessInter.access_listUsername,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                data:{
                    "keywords":value
                },
                success: function (data) {
                    console.log(data.userName);
                    if(value!='')
                    {
                        $(".userlist").removeClass('hide');
                        for (var i in data.userName) {
                            $("#userList").append('<li id="'+data.userName[i].USER_ID+'" class="users">'+data.userName[i].NAME+'</li>');
                        }
                    }
                    else{
                        document.getElementsByClassName('userlist')[0].classList.add('hide');
                    }
                    $('.users').on('click',function () {
                        $('#sendCouponUser').val(this.innerHTML);
                    })
                }
            });
        }
    },0)
})

//审核人
var flag2 = true;
$('#auditPerson').on('compositionstart',function(){
    flag2 = false;
})
$('#auditPerson').on('compositionend',function(){
    flag2 = true;
})
$('#auditPerson').on('input',function(){
    var _this = this;
    setTimeout(function(){
        if(flag2){
            var value=$(_this).val();
            $('li').remove();
            $.ajax({
                url: root.accessInter.access_listUsername,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                data:{
                    "keywords":value
                },
                success: function (data) {
                    console.log(data.userName);
                    if(value!='')
                    {
                        $(".auditlist").removeClass('hide');
                        for (var i in data.userName) {
                            $("#auditList").append('<li id="'+data.userName[i].USER_ID+'" class="audit">'+data.userName[i].NAME+'</li>');
                        }
                    }
                    else{
                        document.getElementsByClassName('auditlist')[0].classList.add('hide');
                    }
                    $('.audit').on('click',function () {
                        $('#auditPerson').val(this.innerHTML);
                    })
                }
            });
        }
    },0)
})

$(document).click(function(e) {
    var target = $(e.target);
    if(target[0].id!='sendCouponDept'){
        $('.depts').remove();
        $('.deptlist').addClass('hide');
    }
    if(target[0].id!='sendCouponUser'){
        $('.users').remove();
        $('.userlist').addClass('hide');
    }
    if(target[0].id!='auditPerson'){
        $('.audit').remove();
        $('.auditlist').addClass('hide');
    }

});


//发券原因二级联动
var oldVal ='';
function sendDeptChange(Val){
    if(oldVal!=Val){
        $('#sendCouponReason').val('')
    }
    oldVal=Val;
    console.log(Val);
    var option = document.getElementsByClassName('options');
    console.log(option);
    switch (parseInt(Val)) {
        case 1:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value==11||option[i].value==12){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 2:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>20&&option[i].value<28){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 3:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>30&&option[i].value<35){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 4:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>40&&option[i].value<49){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 5:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>50&&option[i].value<59){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 6:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>60&&option[i].value<69){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 7:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>70&&option[i].value<79){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
    }
}